// components/selector-multi/selector-multi.js

Component({
  options: {
    addGlobalClass: true
  },
  properties: {
    data: {
      type: Array,
      observer (e) {
        e.forEach((item, index) => {
          if (typeof item === 'object') {
            e[index] = item.tagName
          }
        })
        this.setData({
          _data: e
        })
      }
    },
    _data: {
      type: Array
    },
    checkone: {
      type: Boolean
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    selected: '',
    selectedList: []
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onOptionTap(e) {
      // 选择福利待遇
      const key = e.currentTarget.dataset.item
      const dataList = this.data.selectedList
      if (dataList.indexOf(key) > -1) {
        dataList.splice(dataList.indexOf(key), 1)
      } else {
        dataList.unshift(key)
        if (this.data.checkone) {
          // 如果只能单选
          dataList.length = 1
        }
      }
      this.setData({
        selected: dataList.join(','),
        selectedList: dataList
      })
      this.triggerEvent('checked', this.data.selected)
    },
    reset() {
      this.setData({
        selected: '',
        selectedList: []
      })
    },
  }
})
